// mircoApp/customCluster/polyIndex/polyIndex.js
let app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    ruleShow:false,
    checked:false,
    time:0,
    polyInfo:{},
    categoryId:'',
    page: 1,
    isLoadMore: true,
    total_page: 0,
    groupList:[],
    presentTime:'',
    timeData:{},
    Hei:0,
    imgheights:[],
    imgBoxSize:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.presentTime = new Date().getTime();
    this.data.active = 0;
    this.data.page= 1;
    this.polyIndex();
    // this.groupData();
  },
  slideNavTo(event) {
    let url = event.currentTarget.dataset.url;
    if(url) {
      wx.navigateTo({
        url: `/mircoApp/customCluster/takeList/takeList`
      });
    }
  },
  takeNav() {
    wx.navigateTo({
      url: `/mircoApp/customCluster/takeList/takeList`
    });
  },
  imgH(e){
    var winWid = wx.getSystemInfoSync().windowWidth;//获取当前屏幕的宽度
    var imgh=e.detail.height;//图片高度
    var imgw=e.detail.width;
    var swiperH=winWid*imgh/imgw;　//等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.data.imgheights.push(swiperH);
    this.data.imgBoxSize = Math.max.apply(null, this.data.imgheights);
    this.setData({
        Hei:`${this.data.imgBoxSize}px`//设置高度
    })
  },
  slideNavTo(event) {
    let navTo = event.currentTarget.dataset.url;
    if(navTo) {
      wx.navigateTo({
        url: navTo,
      })
    }
  },
  groupNav(event) {
    console.log(event);
    let group = event.currentTarget.dataset.group;
    wx.navigateTo({
      url: `/mircoApp/customCluster/customList/customList?groupId=${group.id}&title=${group.group_name}`
    });
  },
  ruleConfirm() {
    if(app._isTextEmpty(this.data.checked)) {
      wx.showToast({
        icon: "none",
        title: '请勾选同意规则',
        duration: 1000,
      });
      return false;
    }
    this.setData({
      ruleShow:false
    })
    this.agreeRule();
  },
  onRuleChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  diyNav(event) {
    let navTo = event.currentTarget.dataset.url;
    if(navTo) {
      wx.navigateTo({
        url: navTo,
      })
    }
  },
  categoryTab(event) {
    let id = event.detail.name;
    this.setData({
      categoryId:id,
      isLoadMore:true,
      page:1
    })
    this.groupData();
  },
  endDayChange(event) {
    // let index = event.currentTarget.dataset.index;
    // let groupList =  this.data.groupList.splice(index, 1);
    this.data.page = 1;
    this.groupData();
  },
  onTimeChange(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      [`groupList[${index}].timeData`]: event.detail,
    });
  },
  agreeRule() { //记录用户有没有确认弹窗,只请求
    let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.index.agree-rule");
    app._postNetWork({
      url: urlStr,
      data: {basic_info:1},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        console.log(resdata);
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  polyIndex() { //会员信息
    let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.index.index");
    app._postNetWork({
      url: urlStr,
      data: {basic_info:1},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          let title = res.basic_info?.home?.mailInfo?.name;
          data.rule.desc = data.rule?.desc?.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"'); 
          this.setData({
            categoryId:data.category[0].id,
            polyInfo:data,
            ruleShow:data.rule?.is_show
          })
          this.groupData();
          if(title) {
            wx.setNavigationBarTitle({
              title: title,
            })
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  groupData() {
    let query = {
      page:this.data.page,
      category_id:this.data.categoryId
    };
    let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.group.group-list");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.list?.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          let groupList = data.list.data;
          if(groupList && groupList.length > 0) {
            groupList.forEach((item) => {
              item.timeData = {}
              item.group.buy_max = Number(item.group.buy_max).toFixed(2)
            })
          }
          this.setData({
            groupList: groupList
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
        category_id:this.data.categoryId
      };
      let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.group.group-list");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.list?.data;
          if(list && list.length > 0) {
            list.forEach((item) => {
              item.timeData = {},
              item.group.buy_max = Number(item.group.buy_max).toFixed(2)
            })
          }
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              groupList : this.data.groupList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})